package club.kunlingou.dsc.common.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import club.kunlingou.dsc.common.util.StringUtils;

public class WebResponse {
	
	private String code = FAIL;
	
	private Object data;
	
	private String message = "";
	
	public WebResponse(String code, Object data, String message) {
		super();
		this.code = code;
		this.data = data;
		this.message = message;
	}
	
	public static WebResponse fail() {
		return new WebResponse(FAIL,StringUtils.EMPTY,StringUtils.EMPTY);
	}
	
	public static WebResponse success() {
		return new WebResponse(SUCCESS,StringUtils.EMPTY,StringUtils.EMPTY);
	}
	
	public static WebResponse success(Object data) {
		return new WebResponse(SUCCESS,data,StringUtils.EMPTY);
	}
	
	public String toJSONString() {
		String jsonString = JSON.toJSONString(this,SerializerFeature.WriteDateUseDateFormat);
		return jsonString.replace("\\\\", "\\");
	}
	
	public JSONObject toJSON() {
		return JSON.parseObject(toJSONString());
	}
	
	public static final String SUCCESS = "0";
	
	public static final String FAIL = "-1";

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}
